import datastructure.linklist.ListNode;

/**
 * @Author ZhangCuirong
 * @Date 2025/8/12 12:49
 * @description:
 */
public class Solution203 {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(-1, head);
        ListNode p = dummy;
        while (p.next != null) {
            if (p.next.val == val) {
                p.next = p.next.next;
            }else{
                p=p.next;
            }
        }
        return dummy.next;
    }

    public static void main(String[] args) {
        Solution203 solution = new Solution203();
        ListNode head = ListNode.arrayToList(new int[]{1, 2, 6, 3, 4, 5, 6});
        ListNode newHead = solution.removeElements(head, 6);
        ListNode.printListWithCycleCheck(newHead);
    }
}
